package com.inossem.warehousestorage_inossem.activity.query;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.query.extra.QueryExtra;
import com.inossem.warehousestorage_inossem.adapter.query.QueryOutStorageDetailAdapter;
import com.inossem.warehousestorage_inossem.bean.query.QueryOutStorageDetailBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.query.QueryOutStorageDetailRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.QueryUtils;

import java.util.List;

//出库查询详情
public class QueryOutStorageDetailActivity extends BaseActivity {

    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView positionList;

    private List<QueryOutStorageDetailBean.DataBean.PositionListBean> allData;

    private QueryOutStorageDetailAdapter adapter;

    private String mPosition, mOutStorageNumber;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_query_out_storage_detail);
        initView(QueryOutStorageDetailActivity.this);
        initData(QueryOutStorageDetailActivity.this);
    }

    @Override
    protected void onResumeImpl() {

    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        positionList = (PullableListView) activity.findViewById(R.id.positionList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.positionListRefresh);
    }

    private void initData(Activity activity) {
        title.setText("出库详情");
        ActivityUtils.initBackAndFunction(activity, "出库列表", new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        positionList.setPullUpFlag(false);
        refresh.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                initData(activity);
                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
            }
        });
        getExtra(activity);
        getData(activity, mOutStorageNumber);
        setmScanListener((String scanResult, int length) -> {
            addOutStorageDetailData(activity, QueryUtils.filterPositionList(allData, QRCodeUtils.DecryptQRCode(scanResult)), positionList, true);
        }, QRCodeConstant.TYPE_POSITION);
    }

    private void getExtra(Activity activity) {
        mOutStorageNumber = activity.getIntent().getStringExtra(QueryExtra.OUT_STORAGE_NUMBER);
    }

    private void addOutStorageDetailData(Context context, List<QueryOutStorageDetailBean.DataBean.PositionListBean> positionList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new QueryOutStorageDetailAdapter(context, positionList, mPosition, (String position) -> {
                mPosition = position;
                addOutStorageDetailData(context, QueryUtils.filterPositionList(allData, mPosition), listView, true);
            });
            listView.setAdapter(adapter);
        }
    }

    private void getData(Context context, String outStorageNumber) {
        QueryOutStorageDetailRequest.request(context, outStorageNumber, new GsonBeanListener<QueryOutStorageDetailBean>() {
            @Override
            public void onSuccess(QueryOutStorageDetailBean bean) {
                allData = bean.getData().getPositionList();
                if (allData == null || allData.isEmpty()) {
                    Utils.middleToast(context, "未查到数据");
                }
                addOutStorageDetailData(context, ActivityUtils.getShowData(allData, 0), positionList, true);
            }
        });
    }

    public interface QueryOutStorageDetailListener {
        void onNoDoubleClickListener(String position);
    }

}
